package com.wom.blocks;

import java.awt.Graphics;
import java.util.ArrayList;
import com.wom.blockfeatures.BlockUtils;
import com.wom.gui.BlockTypes;
import com.wom.worldmaker.GameWorld;

public class Tree {
	
	
	public ArrayList<TreeBlock> treeBlocks=new ArrayList<>();
	public int trunkGrowth=0;
	public static int maxTrunkGrowth=6;
	public int leafGrowth=0;
	public int maxleafGrowth=5;
	public static int[] trunk;
	public static ArrayList<TreeLeavesBlock> leaves=new ArrayList<>();
	public int x;
	public int y;
	
	public Tree(int x, int y){
		
		this.x=x;
		this.y=y;
		
		makeTree();
		
	}
	
	
		
	
	
		
	
		
	
	
private void makeTree() {
	
	if(x<3){x=4;}
	if(x>GameWorld.gameBlocks.length-4){x=GameWorld.gameBlocks.length-6;}
	
		treeBlocks.add(new TreeBlock(x, y));
		treeBlocks.add(new TreeBlock(x, y-1));
		treeBlocks.add(new TreeBlock(x, y-2));
		treeBlocks.add(new TreeBlock(x, y-3));
		GameWorld.gameBlocks[x][y]=treeBlocks.get(0);
		GameWorld.gameBlocks[x][y-1]=treeBlocks.get(1);
		GameWorld.gameBlocks[x][y-2]=treeBlocks.get(2);
		GameWorld.gameBlocks[x][y-3]=treeBlocks.get(3);
		
		if(GameWorld.gameBlocks[x][y-3-1] instanceof AirBlock){
			if(GameWorld.gameBlocks[x-1][y-3] instanceof AirBlock){
				if(GameWorld.gameBlocks[x+1][y-3] instanceof AirBlock){
					leaves.add(new TreeLeavesBlock(treeBlocks.get(3).getxPos(), treeBlocks.get(3).getyPos()-1));
		leaves.add(new TreeLeavesBlock(treeBlocks.get(3).getxPos()-1, treeBlocks.get(3).getyPos()));
		leaves.add(new TreeLeavesBlock(treeBlocks.get(3).getxPos()+1, treeBlocks.get(3).getyPos()));
		
		
		BlockUtils.changeInto(treeBlocks.get(3).getxPos(), treeBlocks.get(3).getyPos()-1,BlockTypes.leafBlock);
		BlockUtils.changeInto(treeBlocks.get(3).getxPos()-1, treeBlocks.get(3).getyPos(),BlockTypes.leafBlock);
		BlockUtils.changeInto(treeBlocks.get(3).getxPos()+1, treeBlocks.get(3).getyPos(),BlockTypes.leafBlock);
		
				}
			}
		}
				
		
		
		
	}



public void renderTree(Graphics g){
	for(TreeBlock tb:treeBlocks ){
		tb.renderBlock(g);
	}
	
	for(TreeLeavesBlock l:leaves){
		l.renderBlock(g);
	}
}

public void updateTree(){
	
	//if its leaves are dead remove them from leaves array
	for(TreeLeavesBlock l:leaves){
		if(l.breakable.isDead){
			leaves.remove(l);
		}
	}
}












	
	
	

}
